//
//  MBDGroupCommentsReplyCell.swift
//  MasterBoy
//
//  Created by kyson on 2023/11/23.
//

import Foundation


import UIKit
import YYText
import SnapKit
import SwiftyJSON
import LGAlertView
import Alamofire
import Kingfisher
import Toast_Swift
import JLRoutes


import RxSwift
import RxCocoa

class MBDGroupCommentsReplyCell : MBDCommonCell {
    var avatarImageView : UIImageView!
    //发布人
    var posterLabel : YYLabel!
    //评论内容
    var commontsLabel : YYLabel!
    //评论时间
    var timeLabel : YYLabel!
    
    var replyButton : UIButton!
    
    var praiseButton : UIButton!
    
    private var disposeBag : DisposeBag!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    override var cellModel: Any!{
        didSet {
            let realCellModel = self.cellModel as! MBDGroupCommentsReplyCellModel

            self.posterLabel.textLayout = realCellModel.groupPosterUserNameLayout
            self.commontsLabel.textLayout = realCellModel.groupCommentsLayout
            self.timeLabel.textLayout = realCellModel.groupCommentsTimeLayout
            praiseButton.setTitle(realCellModel.likeCount, for: .normal)
            
            if(true == realCellModel.liked) {
                praiseButton.setImage(UIImage(named: "ico_gdetail_praised"), for: .normal)
            } else {
                praiseButton.setImage(UIImage(named: "ico_gdetail_praise"), for: .normal)
            }

            commontsLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.groupCommentsLayout?.textBoundingSize.height ?? 0)
            }
            
            timeLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.groupCommentsTimeLayout?.textBoundingSize.height ?? 0)
            }
            
            replyButton.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.groupCommentsTimeLayout?.textBoundingSize.height ?? 0)
            }
            
            if let avatar = realCellModel.posterAvatar {
                if avatar.hasPrefix(MBDPath2URLUtils.filePathPrefix) {
                    let imageName = avatar.replacingOccurrences(of: MBDPath2URLUtils.filePathPrefix, with: "")
                    
                    avatarImageView.image = UIImage(named: imageName)
                } else {
                    let url = URL.init(string: MBDPath2URLUtils.wrapAvatarURL(path: avatar))
                    avatarImageView.kf.setImage(with: url)
                }
            } else {
                debugPrint("---avatar 为空")
            }
            

        }
    }

    func initialize() {
        self.commonBackgroundView.setDynamicForgroundColor()
        self.contentView.setDynamicForgroundColor()

        avatarImageView = UIImageView()
        avatarImageView.layer.cornerRadius = 12
        avatarImageView.clipsToBounds = true
        avatarImageView.isUserInteractionEnabled = true
        
        disposeBag = DisposeBag()
        let tapGesture = UITapGestureRecognizer()
        avatarImageView.addGestureRecognizer(tapGesture)
        avatarImageView.image = UIImage(named: "AppIcon")
        tapGesture.rx.event.bind(onNext: { recognizer in
            //回调
            let realCellModel = self.cellModel as! MBDGroupCommentsReplyCellModel
            if let avatarClicedBlock = realCellModel.avatarClicedBlock {
                avatarClicedBlock(realCellModel)
            }
            
        }).disposed(by: disposeBag)
        
        self.commonBackgroundView.addSubview(avatarImageView)
        
        posterLabel = YYLabel()
        self.commonBackgroundView.addSubview(posterLabel)
        
        commontsLabel = YYLabel()
        self.commonBackgroundView.addSubview(commontsLabel)
        
        praiseButton = UIButton()
        praiseButton.setImage(UIImage(named: "ico_gdetail_praise"), for: .normal)
        praiseButton.setTitle("", for: .normal)
        praiseButton.setTitleColor(.gray, for: .normal)
        praiseButton.titleLabel?.font = .systemFont(ofSize: 12)
        praiseButton.addTarget(self, action: #selector(praiseClicked), for: .touchUpInside)
        self.commonBackgroundView.addSubview(praiseButton)
        
        timeLabel = YYLabel()
        self.commonBackgroundView.addSubview(timeLabel)
        
        replyButton = UIButton()
        replyButton.setTitle("回复", for: .normal)
        replyButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
        replyButton.setTitleColor(.gray, for: .normal)
        replyButton.addTarget(self, action: #selector(replyButtonClicked), for: .touchUpInside)
        self.commonBackgroundView.addSubview(replyButton)
                
        avatarImageView.snp.makeConstraints { make in
            make.top.equalTo(MBDGroupCommentsReplyCellModel.avatarMarginTop)
            make.size.equalTo(CGSize(width: MBDGroupCommentsReplyCellModel.avatarWidth, height: MBDGroupCommentsReplyCellModel.avatarWidth))
            make.left.equalTo(MBDGroupCommentsReplyCellModel.avatarMarginLeftToSuperView)
        }
        
        posterLabel.snp.makeConstraints { make in
            make.centerY.equalTo(avatarImageView)
            make.left.equalTo(avatarImageView.snp.right).offset(MBDGroupCommentsReplyCellModel.marginLeftAvatarAndPoster)
            make.right.equalToSuperview().offset(-MBDGroupCommentsReplyCellModel.posterMarginRight)
        }
        
        commontsLabel.snp.makeConstraints { make in
            make.left.equalTo(posterLabel)
            make.top.equalTo(avatarImageView.snp.bottom).offset(MBDGroupCommentsReplyCellModel.commentsMarginTop)
            make.right.equalTo(praiseButton.snp.left)
            make.height.equalTo(0)
        }
        
        praiseButton.snp.makeConstraints { make in

            make.top.equalTo(avatarImageView.snp.bottom).offset(MBDGroupCommentsCellModel.commentsMarginTop)
            make.right.equalTo(self.commonBackgroundView).offset(-8)
            make.size.equalTo(CGSizeMake(MBDGroupCommentsReplyCellModel.praiseButtonWidth, 33))
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(posterLabel)
            make.top.equalTo(commontsLabel.snp.bottom).offset(MBDGroupCommentsReplyCellModel.commentsTimeMarginTop)
            make.height.equalTo(0)
        }
        
        replyButton.snp.makeConstraints { make in
            make.left.equalTo(timeLabel.snp.right).offset(5)
            make.top.equalTo(timeLabel)
            make.width.equalTo(40)
            make.height.equalTo(0)
        }
    }
    
    @objc func replyButtonClicked() {
        let realCellModel = self.cellModel as! MBDGroupCommentsReplyCellModel
        if let replyButtonClicedBlock = realCellModel.replyButtonClicedBlock {
            replyButtonClicedBlock(realCellModel)
        }
    }
    
    @objc func praiseClicked() {
        let realCellModel = self.cellModel as! MBDGroupCommentsReplyCellModel
        if let replyPraiseButtonClicedBlock = realCellModel.replyPraiseButtonClicedBlock {
            replyPraiseButtonClicedBlock(realCellModel)
        }
    }
}



